const App = getApp();
const func = require('../../../utils/func');

Page({

    /**
     * 页面的初始数据
     */
    data: { 
        // 售后订单ID
        service_id: 0,
        // 售后订单信息
        orderGoods: {},
        // 是否正在加载中，默认是
        isLoading: true,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _this = this;
        // 设置数据类型
        _this.setData({
            service_id: parseInt(options.service_id) > 0 ? parseInt(options.service_id) : 0,
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let _this = this;
        // 查询订单商品是否可申请售后
        _this.getOrderGoodsService();
    },

    // 下拉刷新，重载数据
    onPullDownRefresh() {
        let _this = this;
        // 查询订单商品是否可申请售后
        _this.getOrderGoodsService();
        wx.stopPullDownRefresh();
    },

    // 查询订单商品是否可申请售后
    getOrderGoodsService: function() {
        let _this = this;
        let postData = {
            action: 'getService',
            service_id: _this.data.service_id,
        };
        App._requestApi(_this, App.globalData.config.shopOrderServiceActionUrl, postData,
            function (res) {
                _this.setData({
                    isLoading: false,
                    address: res.data.address ? res.data.address : [],
                    express: res.data.express ? res.data.express : [],
                    orderGoods: res.data.orderGoods ? res.data.orderGoods : [],
                    service: res.data.service ? res.data.service : [],
                    serviceLog: res.data.serviceLog ? res.data.serviceLog : [],
                    serviceType: res.data.serviceType ? res.data.serviceType : [],
                });
            },
            function (res) {
                App.showError(res.msg, function() {
                    wx.navigateBack({delta: 1});
                });
            }
        );
    },

    // 取消售后服务订单
    cancelService() {
        let _this = this;
        wx.showModal({
            title: "友情提示",
            content: "确认要关闭维权服务吗？",
            success(o) {
                if (o.confirm) {
                    var order_id = _this.data.orderGoods.order_id;
                    var details_id = _this.data.orderGoods.details_id;
                    var service_type = _this.data.service.service_type;
                    App._requestPost(_this, App.globalData.config.shopOrderServiceActionUrl, {
                        order_id: order_id,
                        details_id: details_id,
                        action: 'cancelService',
                        service_type: service_type,
                        service_id: _this.data.service_id
                    }, result => {
                        App.showSuccess(result.msg, function() {
                            wx.navigateBack({delta: 1});
                        });
                    });
                }
            }
        });
    },

    // 订单商品维权
    applyService(e) {
        let _this = this;
        var order_id = _this.data.orderGoods.order_id;
        var details_id = _this.data.orderGoods.details_id;
        let product_id = _this.data.orderGoods.product_id;
        let postData = {
            use: 'service',
            order_id: order_id,
            action: 'goodsDetect',
            details_id: details_id,
            product_id: product_id,
        };
        App._requestApi(_this, App.globalData.config.shopOrderServiceActionUrl, postData, function (res) {
            let jumpUrl;
            if (parseInt(res.data.apply_service) === 1 && parseInt(res.data.service_id) !== 0) {
                jumpUrl = '../service/detail?service_id=' + res.data.service_id;
            } else {
                jumpUrl = '../service/apply?order_id=' + order_id + '&details_id=' + details_id + '&product_id=' + product_id
            }
            wx.navigateTo({url: jumpUrl});
        });
    },

    // 买家确认收到商品
    confirmService() {
        let _this = this;
        wx.showModal({
            title: "友情提示",
            content: "确认已收到商品？",
            success(o) {
                if (o.confirm) {
                    var order_id = _this.data.orderGoods.order_id;
                    var details_id = _this.data.orderGoods.details_id;
                    var service_type = _this.data.service.service_type;
                    App._requestPost(_this, App.globalData.config.shopOrderServiceActionUrl, {
                        order_id: order_id,
                        details_id: details_id,
                        action: 'confirmService',
                        service_type: service_type,
                        service_id: _this.data.service_id
                    }, result => {
                        App.showSuccess(result.msg, function() {
                            _this.getOrderGoodsService();
                        });
                    });
                }
            }
        });
    },

    // 跳转到其他界面
    jumpToView(e) {
        func.jumpToUrl(e)
    },
})